module ResultsetFilterHelper
include ResultsetAutofilter

  def get_value(page, val)
    Filters_and_conditions.get_value(nil, page, val)
  end


  def resultset_filter(options = {})

    type_model = options[:type].to_s + '_' + options[:model].to_s
    controller_page = controller.controller_name + '_' + options[:page].to_s
    type_model_field = type_model + '_' + options[:field].to_s
    field = options[:field]

    sel_selected = Filters_and_conditions.get_value(nil, controller_page.to_sym, type_model_field)

    case options[:type]
    when :selectandinput
      filter_sort = hash_sort(options[:filter])
      input_model = 'input_' + options[:model].to_s
      input_model_field = input_model + '_' + options[:field].to_s


      input_selected = Filters_and_conditions.get_value(nil, controller_page.to_sym, input_model_field)

      if options[:js] != ""
        option_js = {}
        options[:js].each do |key, value|
          option_js[key] = value
        end
        #  out = select(type_model.to_sym, field, filter_sort, options = {:selected => session[controller_page.to_sym].send(type_model_field)}, html_options = option_js)
        out = select(type_model.to_sym, field, filter_sort, options = {:selected => sel_selected}, html_options = option_js)
      else
        #  out = select(type_model.to_sym, field, filter_sort, options = {:selected => session[controller_page.to_sym].send(type_model_field)})
        out = select(type_model.to_sym, field, filter_sort, options = {:selected => sel_selected})
      end
      #      out << text_field(input_model.to_sym, field, :size => 30, :value => session[controller_page.to_sym].send(input_model_field))
      #      out << text_field(input_model.to_sym, field, :size => 30, :value => gc.send(input_model_field))

      out << text_field(input_model.to_sym, field, :size => 30, :value => input_selected)
      out << observe_field(input_model_field.to_sym, :frequency => 0.5, :function => "javascript:$('form_search').onsubmit()")

    when :select
      filter_sort = hash_sort(options[:filter]) {|data| data['-- Tout --'] = '-1' }
      if options[:js] != ""
        option_js = {}
        options[:js].each do |key, value|
          option_js[key] = value
        end
      #        out = select(type_model.to_sym, field, filter_sort, options = {:selected => session[controller_page.to_sym].send(type_model_field)}, html_options = option_js)

        out = select(type_model.to_sym, field, filter_sort, options = {:selected => sel_selected}, html_options = option_js)

      else
        #  out = select(type_model.to_sym, field, filter_sort, options = {:selected => session[controller_page.to_sym].send(type_model_field)})
        out = select(type_model.to_sym, field, filter_sort, options = {:selected => sel_selected})
      end


    when :selectwithbool
      filter_sort = hash_sort(options[:filter]) {|data| data['-- Tout --'] = '-1' }
      if options[:js] != ""
        option_js = {}
        options[:js].each do |key, value|
          option_js[key] = value
        end
      # out = select(type_model.to_sym, field, filter_sort, options = {:selected => session[controller_page.to_sym].send(type_model_field) ? session[controller_page.to_sym].send(type_model_field) : '-1'}, html_options = option_js)
      out = select(type_model.to_sym, field, filter_sort, options = {:selected => sel_selected ? sel_selected : '-1'}, html_options = option_js)
      else
       # out = select(type_model.to_sym, field, filter_sort, options = {:selected => session[controller_page.to_sym].send(type_model_field) ? session[controller_page.to_sym].send(type_model_field) : '-1'})
        out = select(type_model.to_sym, field, filter_sort, options = {:selected => sel_selected ? sel_selected : '-1'})
      end


    end
      out
  end


  ######
    private
  ######

  def hash_sort(options_filter)
    filter = Hash.new

    yield filter if block_given?

    options_filter.each do |key, val|
      filter[key] = val.to_s
    end

    filter.sort_by {|key, val| key.tr(' ','').strip.downcase}
  end


end
